#!/bin/sh

echo "Waiting for postgres..."

#while ! nc -z users-db 5432; do
#    sleep 0.1
#done
sleep 5

echo "PostgreSQL started"
echo "Waiting for makemigrate..."

# Move migration to bash file.
python3 manage.py makemigrations accounts
python3 manage.py makemigrations analysis
python3 manage.py makemigrations material
python3 manage.py makemigrations mlplatform
python3 manage.py makemigrations search
python3 manage.py makemigrations storage
python3 manage.py makemigrations

echo "makemigrate complate!"
echo "Waiting for migrate..."
python3 manage.py migrate


echo "makemessages"
python3 manage.py makemessages
python3 manage.py makemessages -d djangojs
python3 manage.py compilemessages

echo "add index to es"

curl -XPUT "http://hte_elasticsearch:9200/data_content" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "_doc": {
      "properties": {
        "id": {
          "type": "keyword",
          "ignore_above": 64
        },
        "_meta_id": {
          "type": "long"
        },
        "_tid": {
          "type": "long"
        },
        "p": {
          "type": "nested",
          "properties": {
            "key": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 512
                }
              }
            },
            "lb": {
              "type": "double"
            },
            "ub": {
              "type": "double"
            },
            "val": {
              "type": "double"
            },
            "err": {
              "type": "double"
            },
            "id": {
              "type": "keyword",
              "ignore_above": 64
            },
            "text": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                },
                "reverse": {
                  "type": "text",
                  "analyzer": "reverse_analyzer"
                }
              }
            },
            "num": {
              "type": "double"
            },
            "list": {
              "type": "nested",
              "properties": {
                "lb": {
                  "type": "double"
                },
                "ub": {
                  "type": "double"
                },
                "val": {
                  "type": "double"
                },
                "err": {
                  "type": "double"
                },
                "id": {
                  "type": "keyword",
                  "ignore_above": 64
                },
                "text": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    },
                    "reverse": {
                      "type": "text",
                      "analyzer": "reverse_analyzer"
                    }
                  }
                },
                "num": {
                  "type": "double"
                }
              }
            }
          }
        }
      }
    }
  },
  "settings": {
    "analysis": {
      "analyzer": {
        "reverse_analyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": [
            "reverse",
            "last512"
          ]
        }
      },
      "filter": {
        "last512": {
          "type": "truncate",
          "length": 512
        }
      }
    },
    "index": {
      "max_result_window": 100000000
    }
  }
}'

curl -XPUT "http://hte_elasticsearch:9200/data_field_container" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "_doc": {
      "properties": {
        "id": {"type": "keyword", "ignore_above": 64},
        "_meta_id": {"type": "long"},
        "_tid": {"type": "long"},
        "_path": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword", 
              "ignore_above": 512
            }
          }
        },
        "_owner_id": {"type": "keyword", "ignore_above": 64},
        "p": {
          "type": "nested",
          "properties": {
            "key": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword", 
                  "ignore_above": 512
                }
              }
            },
            "lb": {"type": "double" },
            "ub": {"type": "double" },
            "val": {"type": "double" },
            "err": {"type": "double" },
            "id": {"type": "keyword" , "ignore_above": 64},
            "text": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                },
                "reverse": {
                  "type": "text",
                  "analyzer": "reverse_analyzer"
                }
              }
            },
            "num": {"type": "double" },
            "list": {
              "type": "nested",
              "properties": {
                "lb": {"type": "double" },
                "ub": {"type": "double" },
                "val": {"type": "double" },
                "err": {"type": "double" },
                "id": {"type": "keyword" , "ignore_above": 64},
                "text": {
                  "type": "text", 
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    },
                    "reverse": {
                      "type": "text",
                      "analyzer": "reverse_analyzer"
                    }
                  }
                },
                "num": {"type": "double" }
              }
            }
          }
        }
      }
    }
  },
  "settings": {
    "analysis": {
      "analyzer": {
        "reverse_analyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": ["reverse","last512"]
        }
      },
      "filter": {
        "last512": {
          "type": "truncate",
          "length": 512
        }
      }
    },
    "index": {
      "max_result_window": 100000000
    } 
  }
}'

curl -XPUT "http://hte_elasticsearch:9200/data_field_table" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "_doc": {
      "properties": {
        "id": {"type": "keyword", "ignore_above": 64},
        "_meta_id": {"type": "long"},
        "_tid": {"type": "long"},
        "_path": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 512
            }
          }
        },
        "_owner_id": {"type": "keyword", "ignore_above": 64},
        "p": {
          "type": "nested",
          "properties": {
            "key": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 512
                }
              }
            },
            "num": {"type": "long"},
            "list": {
              "type": "nested",
              "properties": {
                "id": {"type": "keyword" , "ignore_above": 64},
                "text": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    },
                    "reverse": {
                      "type": "text",
                      "analyzer": "reverse_analyzer"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "settings": {
    "analysis": {
      "analyzer": {
        "reverse_analyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": ["reverse","last512"]
        }
      },
      "filter": {
        "last512": {
          "type": "truncate",
          "length": 512
        }
      }
    },
    "index": {
      "max_result_window": 100000000
    }
  }
}'

curl -XPUT "http://hte_elasticsearch:9200/data_field_table_row" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "_doc": {
      "properties": {
        "id": {"type": "keyword", "ignore_above": 64},
        "_meta_id": {"type": "long"},
        "_tid": {"type": "long"},
        "_path": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 512
            }
          }
        },
        "_owner_id": {"type": "keyword", "ignore_above": 64},
        "p": {
          "type": "nested",
          "properties": {
            "key": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 512
                }
              }
            },
            "lb": {"type": "double" },
            "ub": {"type": "double" },
            "val": {"type": "double" },
            "err": {"type": "double" },
            "id": {"type": "keyword" , "ignore_above": 64},
            "text": {
              "type": "text" ,
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                },
                "reverse": {
                  "type": "text",
                  "analyzer": "reverse_analyzer"
                }
              }
            },
            "num": {"type": "double" },
            "list": {
              "type": "nested",
              "properties": {
                "lb": {"type": "double" },
                "ub": {"type": "double" },
                "val": {"type": "double" },
                "err": {"type": "double" },
                "id": {"type": "keyword" , "ignore_above": 64},
                "text": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    },
                    "reverse": {
                      "type": "text",
                      "analyzer": "reverse_analyzer"
                    }
                  }
                },
                "num": {"type": "double" }
              }
            }
          }
        }
      }
    }
  },
  "settings": {
    "analysis": {
      "analyzer": {
        "reverse_analyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": ["reverse","last512"]
        }
      },
      "filter": {
        "last512": {
          "type": "truncate",
          "length": 512
        }
      }
    },
    "index": {
      "max_result_window": 100000000
    }
  }
}'

curl -XPUT "http://hte_elasticsearch:9200/data_meta" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "_doc": {
      "properties": {
        "p": {
          "type": "nested",
          "properties": {
            "key": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },

            "text": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                },
                "reverse": {
                  "type": "text",
                  "analyzer": "reverse_analyzer"
                }
              }
            },

            "num": {
              "type": "double"
            },

            "list": {
              "type": "nested",
              "properties": {
                "text": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    },
                    "reverse": {
                      "type": "text",
                      "analyzer": "reverse_analyzer"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "settings": {
    "analysis": {
      "analyzer": {
        "reverse_analyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": ["reverse","last512"]
        }
      },
      "filter": {
        "last512": {
          "type": "truncate",
          "length": 512
        }
      }
    },
    "index": {
      "max_result_window": 100000000
    }
  }
}'

curl -XPUT "http://hte_elasticsearch:9200/data_snapshot" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "_doc": {
      "properties": {
        "_meta_id": {
          "type": "long"
        },
        "_content": {
          "type": "text"
        }
      }
    }
  },
  "settings": {
    "max_result_window": 100000000
  }
}'

python3 manage.py shell -c "from apps.search.core.es_handler import ElasticsearchManager;ElasticsearchManager.update_all()"


gunicorn hte.wsgi -b 0.0.0.0:5000
